package com.ssm.zy.service.impl;

import com.ssm.zy.mapper.FollowMapper;
import com.ssm.zy.pojo.User;
import com.ssm.zy.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private FollowMapper followMapper;
    @Override
    public List<User> checkUserFocus(Integer userId) {
        return followMapper.checkUserFocus(userId);
    }

    @Override
    public List<User> checkPassiveFocus(Integer userId) {
        return followMapper.checkPassiveFocus(userId);
    }

    @Transactional
    @Override
    public int addfollowUser(Integer userId, Integer id) {
        if(userId.equals(id)){//自己不能关注自己
            return 0;
        }
        List<Integer> userIdList=followMapper.getPassiveFocusId(id);
        int i=0;
        int j=0;
        int k=0;
        if(userIdList.contains(userId)){//包含说明已经关注，需要取消关注
            i=followMapper.delfollow(userId,id);
            j=followMapper.delfollowUser(userId);
            k=followMapper.delfollowUsers(id);
        }else{//不包含说明没有关注
            i=followMapper.addfollow(userId,id);
            j=followMapper.addfollowUser(userId);
            k=followMapper.addfollowUsers(id);
        }
        if(i>0 && j>0 && k>0){
            return i+j+k;
        }
        return 0;
    }
}
